package com.magelink.controller;


import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.json.JSONUtil;
import com.magelink.config.IsvConfig;
import com.magelink.core.ResponseMsg;
import com.magelink.domain.ActivityEnum;
import com.magelink.pojo.IsvInstance;
import com.magelink.service.IsvService;
import com.magelink.util.SecureUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;


/**
 * 接入华为云市场实现新购商品 商品续费 商品过期 商品资源释放 商品升级
 * 文档:https://support.huaweicloud.com/accessg-marketplace/zh-cn_topic_0070649013.html
 *
 * @author 梁卓
 */
//@Api(tags = "saas商品接入")
@RequestMapping("/produceAPI")
@RestController
@AllArgsConstructor
@Slf4j
public class IsvController {

    @Autowired
    private IsvService isvService;

    @Autowired
    private IsvConfig isvConfig;



    @ApiOperation("saas商品接入,客户购买商品后,将请求本接口")
    @GetMapping
//    ("/marketplace/saas")
    public String instance(IsvInstance instance, HttpServletResponse response) {
        System.out.println("1112");
        ResponseMsg responseBody = isvService.instance(instance);
        String signature = null;
        String jsonStr = null;
        try {
            // service返回的响应转换为json字符串
            jsonStr = JSONUtil.toJsonStr(responseBody);
            // 对响应json字符串进行签名
            signature = SecureUtils.generateResponseBodySignature(isvConfig.getAccessKey(), jsonStr);
        } catch (Exception e) {
            log.error("生成响应body签名异常:{}", ExceptionUtil.getMessage(e));
        }
        // 签名放入HttpServletResponse 响应头
        response.addHeader("Body-Sign", "sign_type=\"HMAC-SHA256\"" + "," + "signature=" + "\"" + signature + "\"");
        return jsonStr;
    }


//
//    /**
//     * 新购场景
//     */
//    @GetMapping
//    public ResponseMsg newInstance(IsvInstance isvInstance) {
 //          ResponseMsg responseBody = new ResponseMsg();
//        responseBody = isvService.instance(isvInstance);
//        log.info("新购场景:");
//        return responseBody;
//
//    }
//
//    /**
//     * 续费场景
//     */
//    @GetMapping
//    private void refreshInstanceScene() {
//        log.info("续费场景:");
//    }

//    /**
//     * 商品过期
//     */
//    private void expireInstanceScene() {
//        log.info("商品过期场景:");
//    }
//
//    /**
//     * 商品资源释放
//     */
//    private void releaseInstanceScene() {
//        log.info("商品资源释放场景:");
//    }
//
//    /**
//     * 商品升级
//     */
//    private void upgradeScene() {
//        log.info("商品升级场景:");
//    }
//
//    /**
//     * 实例是否已存在,并且有效，
//     *
//     * @param orderId
//     * @return
//     */
//    private boolean isExist(String orderId) {
//
//        return true;
//    }



    /**
     * 新购场景
     */
//    public void newInstanceScene(IsvInstance isvInstance) {
////        云市场业务ID
//        String businessId = isvInstance.getBusinessId();
////        过期时间
//        String expireTime = isvInstance.getExpireTime();
////        云市场订单ID
//        String orderId = isvInstance.getOrderId();
////        产品标识
//        String productId = isvInstance.getProductId();
////        当前时间并且转换"yyyyMMddHHmmss"
////        String timeStamp = DateUtil.format(new Date(isvInstance.getTimeStamp()), "yyyyMMddHHmmss");
//        String timeStamp = isvInstance.getTimeStamp();
////       是否是开通试用实例
//        String trialFlag = isvInstance.getTrialFlag();
////        传入的token
//        String authToken = isvInstance.getAuthToken();
//        JSONObject josn = new JSONObject();
//        try {
//            josn.put("businessId", businessId);
//            josn.put("expireTime", expireTime);
//            josn.put("orderId", orderId);
//            josn.put("productId", productId);
//            josn.put("timeStamp", timeStamp);
//            josn.put("trialFlag", trialFlag);
//            josn.put("authToken", authToken);
//        } catch (Exception e) {
//
//        }
//        System.out.println(josn.toString());
//
//    }

}
